package com.xszx.service.impl;

import com.xszx.entity.Goods;
import com.xszx.entity.Role;
import com.xszx.entity.Shopping;
import com.xszx.entity.User;
import com.xszx.mapper.AdminMapper;
import com.xszx.mapper.SystemMapper;
import com.xszx.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author:瓦达西瓦
 * @date:2024/10/31
 */
@Service
public class SystemServiceImpl implements SystemService {
    @Autowired
  private SystemMapper systemMapper;

    @Autowired
    private AdminMapper adminMapper;



    @Override
    public List<Goods> getSort(Integer gtid) {
        List<Goods> list=systemMapper.getSort(gtid);
        return list;
    }

    @Override
    public List<Role> getrole() {
        List<Role> list=systemMapper.getrole();
        return list;
    }

    @Override
    public List<Shopping> getShoppping(User user) {
        List<Shopping> list=systemMapper.getShoppping(user);
        return list;

    }

    @Override
    public List<Goods> getShoppping02(List<Shopping> shoppingList) {
        List<Goods> list=   systemMapper.getShoppping02(shoppingList);
        return list;
    }

    @Override
    public List<Goods> getYouLove(List<Goods> shoppingList) {
        List<Goods> list=  systemMapper.getYouLove(shoppingList);
        return list;

    }

    @Override
    public Double getMoneySum() {

       Double sum=  systemMapper.getMoneySum();
        return sum;
    }

    @Override
    public Integer getOrdersSum() {
     Integer sum= systemMapper.getOrdersSum();
        return sum;
    }

    @Override
    public Integer getuserSum() {
      Integer sum=  systemMapper.getuserSum();
        return sum;
    }

    @Override
    public List<Goods> getGoodsSearch(String search) {
        List<Goods> goodsList=new ArrayList<>();
//        通过商品名字查询
        List<Goods> goodsGList=systemMapper.getGoodsSearch(search);
        for (int i = 0; i < goodsGList.size(); i++) {
            goodsList.add(goodsGList.get(i));
        }
//        通过卖家名字查询
        List<Goods> goodsNList=systemMapper.getNameSearch(search);
        for (int i = 0; i < goodsNList.size(); i++) {
            boolean found = false;
            for (int j = 0; j < goodsList.size(); j++) {
                if(goodsList.get(j).getGid()==goodsNList.get(i).getGid()){
                    found =true ;
                    break;
                }
            }
            if(!found==true){goodsList.add(goodsNList.get(i));}
        }
//        将字符串拆分为单个字段
        char [] str=search.toCharArray();
//        通过单个字段查询商品名
        for (int m = 0; m < str.length; m++) {
            List<Goods> goodsGFList=systemMapper.getGFSearch(str[m]);
            for (int i = 0; i < goodsGFList.size(); i++) {
                boolean found = false;
                for (int j = 0; j < goodsList.size(); j++) {
                    if(goodsList.get(j).getGid()==goodsGFList.get(i).getGid()){
                        found =true ;
                        break;
                    }
                }
                if(!found==true){goodsList.add(goodsGFList.get(i));}
            }
        }
//        通过单个字段查询卖家名
        for (int m = 0; m < str.length; m++) {
            List<Goods> goodsNFList=systemMapper.getNFSearch(str[m]);
            for (int i = 0; i < goodsNFList.size(); i++) {
                boolean found = false;
                for (int j = 0; j < goodsList.size(); j++) {
                    if(goodsList.get(j).getGid()==goodsNFList.get(i).getGid()){
                        found =true ;
                        break;
                    }
                }
                if(!found==true){goodsList.add(goodsNFList.get(i));}
            }
        }
//        将未查询到的信息放到队尾
            List<Goods> goods=adminMapper.getGoodList();
            for (int i = 0; i < goods.size(); i++) {
                boolean found = false;
                for (int j = 0; j < goodsList.size(); j++) {
                    if(goodsList.get(j).getGid()==goods.get(i).getGid()){
                        found =true ;
                        break;
                    }
                }
                if(!found==true){goodsList.add(goods.get(i));}
            }
        return goodsList;
    }




}
